define(function(require, exports, module) {
  var $ = require("$");
  var dtUtil = require('datatableutil');
  var loader = require("./loader/loader");
  require('js/core/jquery/src/jquery_resize');
  require("bootstrap_commonrequire");
  require("moment");
  require('daterangepicker.css');
  require('bootstrap-daterangepicker');
  require('select2.css');
  require('select2');
 
  exports.init = function(param) {

    $(document).ready(function() {
      
      loader.loading("数据加载中……");

      // 加载自定义模板-条件区域
      var template = require("./logRepushTicket.tpl");
      var Widget = require("widget");
      var Component = Widget.extend({
        template : template,
      });
      new Component({
        renderTo : "queryAreaId"
      });

      // 初始化日期范围选择控件
      var DateRange = require('daterangeinput');
      var daterangeAttrs = {
        daterangeId : "querydatewrapper_rang",
        label : "时间"
      };
      var daterangeCom = new DateRange({
        attrs : daterangeAttrs,
        renderTo : 'querydatewrapper'
      });

      function getCondition() {
        // 获取查询条件
        var queryStr = "";
        // sessionID
        var session_id = $('#session_id').val();
        if (session_id != '' && session_id != null) {
          queryStr += ("session_id='" + session_id + "';");
        }	
        // 主叫
        var Caller = $('#Caller').val();
        if (Caller != '' && Caller != null) {
          queryStr += ("Caller like '%" + Caller + "%';");
        }
        
        var Callee = $('#Callee').val();
        if (Callee != '' && Callee != null) {
          queryStr += ("Callee like '%" + Callee + "%';");
        }
        // 应用
        var app_key = $('#app_key').val();
        if (app_key != '' && app_key != null) {
          queryStr += ("app_key like '%" + app_key + "%';");
        }
        // 计费账号
        var cust_account = $('#cust_account').val();
        if (cust_account != '' && cust_account != null) {
          queryStr += ("cust_account like '%" + cust_account + "%';");
        }
        // 推送地址
        var record_url = $('#record_url').val();
        if (record_url != '' && record_url != null) {
          queryStr += ("record_url like '%" + record_url + "%';");
        }
        return queryStr;
      }
      // 加载表单模板和数据
      var Table = require("table");
      // 查询内容显示
      new Table({
        attrs : {
          id : "datatableid",
          isHovered : false,
          isStriped : true,
          isBordered : false
        },
        renderTo : param.divWrapperId

      });
      var dtUtil = require('datatableutil');
      // 信息显示
      var data_table = $('#datatableid').dataTable({
        "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "bJQueryUI" : false,
        "oLanguage" : {
          "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
        },
        "sPaginationType" : "full_numbers", // 分页自带两种样式，full_numbers和two_button(默认)
        'bPaginate' : true, // 是否分页
        "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
        "bServerSide" : true, // 指定服务器端分页
        "bDeferRender" : true,
        "bFilter" : true,
        "bSort" : true,
        "bInfo" : true,
        "sAjaxSource" : param.ajaxUrl,
        "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.MIN, // 默认分页的每页记录数
        "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
          var queryparam = dtUtil.convert_aoData(aoData);

          // 拼接where查询条件
          var cond = getCondition();
          if (cond != '' && cond != null) {
            queryparam.q += cond;
          }
          // 添加自己的额外参数
          oSettings.jqXHR = $.ajax({
            "dataType" : 'json',
            "contentType" : "application/json;charset=UTF-8",
            "type" : "GET",
            "url" : sSource,
            "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
            "success" : function(json) {
              loader.noloading();
              if (json.flag == '000000') {
                dtUtil.successCallback(json, aoData, fnCallback);
              } else {
                oauth2Relogin();
              }
            }
          });

        },
        "aoColumns" : [{
          "sTitle" : "能力类型",
          "bSortable" : false,
          "mData" : "ability_type",
          "sWidth" : "20%",
        },{
          "sTitle" : "SessionId",
          "bSortable" : false,
          "sWidth" : "10%",
          "mData" : "session_id"
        },{
            "sTitle" : "应用账号",
            "bSortable" : false,
            "sWidth" : "10%",
            "mData" : "app_key"
        },{
            "sTitle" : "计费账号",
            "bSortable" : false,
            "mData" : "cust_account",
            "sWidth" : "10%",
        },{
          "sTitle" : "被叫",
          "bSortable" : false,
          "mData" : "callee",
          "bVisible" : true,
          "sWidth" : "10%",
        },{
	      "sTitle" : "推送结果",
	      "bSortable" : false,
	      "mData" : "status",
	      "sWidth" : "20%",
	      "bVisible" : true,
          "mRender" : function(data, type, full) {
              if (data == "1")
            	  return "推送失败";
              else if (data == "0")
                  return "待推送";
              else
            	  return data;
           }
	  },{
	      "sTitle" : "失败描述",
	      "bSortable" : false,
	      "mData" : "note", 
	      "sWidth" : "10%",
	      "bVisible" : true
	  },{
	      "sTitle" : "推送地址",
	      "bSortable" : false,
	      "mData" : "record_url", 
	      "sWidth" : "10%",
	      "bVisible" : true,
	      "mRender" : function(data, type, full)
			{
				return "<a href='javascript:void(0);' class='name' style='text-decoration:underline;word-wrap:break-word;width:100px;display:inline-block;'>" + data + "</a>";
			}
	  }],
        "fnDrawCallback" : function(oSettings) {
          // 表格绘制完成后，去除width属性，使表格可缩放
          $('#' + oSettings.sTableId).css("width", "");
        }
      });
      
    
      // 查询按钮
      $('#querybtn').click(function() {
          loader.loading("数据加载中……");
    	  data_table.fnFilter("");
    	  return false;
      });

      // 重置按钮
      $('#restbtn').click(function() {
          loader.loading("数据加载中……");
          $('#Callee').val("");
          $('#Caller').val("");
          $("#session_id").val("");
          // daterangeCom.clearInput();
          data_table.fnFilter("");
          return false;
      });
      
      $('#resendbtn').click(function() {
    	  var confirmed = confirm("此操作会按查询条件重新推送，请确认！");
    	  if(confirmed != true){
    		  bootbox.alert("您已取消重推操作");
    		  return false;
    	  }
    	  
          loader.loading("数据加载中……");
          // 拼接查询条件
          var queryparam = {};
          var cond = getCondition();
          if (cond != '' && cond != null) {
        	  queryparam.q = cond;
          }else{
        	  queryparam.q = null;
          }
          $.ajax({
              url : "/logPushTicket/repushTicket.do",
              dataType : "json",
              "data" : queryparam,
              type : "GET",
              success : function(json) {
                  loader.noloading(); 
                  if(json.flag == '000000'){
                	  $("#data_table").dataTable().fnFilter(); 
                	  bootbox.alert("重推操作成功，共"+json.count+"条，话单数据稍后重新推送！");
                  } else {
                	  loader.noloading(); 
                	  bootbox.alert("重发操作失败！");                 
                  }
              },
              error : function(s) {
                loader.noloading();
                bootbox.alertTimeout("网络请求失败，请重试！");
              }
            });
    	  
      });

    });

  };
});
